//
//  BBProductDetailViewController.swift
//  BBLinkiOSProject
//
//  Created by liuzhu on 2018/12/10.
//  Copyright © 2018 liuzhu. All rights reserved.
//

import LPBannerView
import SDWebImage
import UIKit
class BBProductDetailViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.rightBarButtonItem = UIBarButtonItem.creatBarButtonItem(imageName: "shop_add_cart", target: self, action: #selector(rightItemClick))
        tabBarController?.tabBar.isHidden = true
        setUI()
    }

    var bannerList: [String]? {
        didSet {
            bannerView.imagePaths = bannerList!
        }
    }

    var productDes: [String]? {
        didSet {
            var ivHeight = 0
            for str in productDes! {
                let arr = str.components(separatedBy: ",")
                let width = CGFloat(Float(arr[0])!)
                let height = CGFloat(Float(arr[1])!)
                let ivWidth = UIScreen.main.bounds.size.width
                let scale = ivWidth / width
                let imageWidth = ivWidth
                let imageHeight = height * scale
                let imageView = UIImageView(frame: CGRect(x: CGFloat(0), y: CGFloat(ivHeight), width: CGFloat(imageWidth), height: CGFloat(imageHeight)))
                let url = URL(string: arr[2])
                imageView.sd_setImage(with: url, placeholderImage: UIImage(named: "default_image"), options: SDWebImageOptions(rawValue: 1), completed: nil)
                productDesImages.addSubview(imageView)
                ivHeight = ivHeight + Int(imageHeight)
            }
            let frame = CGRect(x: 0, y: bannerView.frame.origin.y + bannerView.bounds.size.height, width: UIScreen.main.bounds.size.width, height: CGFloat(ivHeight))
            productDesImages.frame = frame
            scroolView.contentSize = CGSize(width: UIScreen.main.bounds.size.width, height: productDesImages.bounds.size.height + bannerView.bounds.size.height)
        }
    }

    @objc private func rightItemClick() {
    }

    private func setUI() {
        view.addSubview(scroolView)
    }

    private lazy var scroolView: UIScrollView = {
        let sv = UIScrollView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))
        sv.addSubview(bannerView)
        sv.addSubview(productDesImages)
        return sv
    }()

    private lazy var bannerView: LPBannerView = {
        let bannerView = LPBannerView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.width / 2))
        bannerView.autoScrollTimeInterval = 3
        bannerView.placeholderImage = UIImage(named: "default_image")!
        return bannerView
    }()

    private lazy var productDesImages: UIView = {
        let iv = UIView()
        return iv
    }()
}
